package com.water.skin

import android.content.res.ColorStateList
import android.content.res.Resources
import android.content.res.Resources.NotFoundException
import android.graphics.drawable.Drawable
import android.text.TextUtils
import com.water.skin.utils.L.e
import pl.droidsonroids.gif.GifDrawable

class ResourceManager(
    private val mResources: Resources,
    private val mPluginPackageName: String,
    private val mSuffix: String?
) {

    /**
     * 获取资源:Gif图片.
     * @param name 资源名称
     * @return GifDrawable
     */
    fun getGifDrawableByName(name: String): GifDrawable? {
        e("name = $name , $mPluginPackageName")
        return try {
            GifDrawable(mResources, getDrawableIdByName(name))
        } catch (e: NotFoundException) {
            e.printStackTrace()
            e("name = $name ,Gif($mPluginPackageName)")
            null
        }
    }

    @Throws(NotFoundException::class)
    private fun getDrawableIdByName(name: String): Int {
        e("name = $name , $mPluginPackageName")
        return mResources.getIdentifier(
            appendSuffix(name).also {
                e("name = $it , $mPluginPackageName")
            },
            DEF_TYPE_DRAWABLE,
            mPluginPackageName
        )
    }

    fun getDrawableByName(name: String): Drawable? {
        e("name = $name , $mPluginPackageName")
        return try {
            mResources.getDrawable(
                mResources.getIdentifier(
                    appendSuffix(name).also {
                        e("name = $it , $mPluginPackageName")
                    },
                    DEF_TYPE_DRAWABLE,
                    mPluginPackageName
                )
            )
        } catch (e: NotFoundException) {
            e.printStackTrace()
            null
        }
    }

    @Throws(NotFoundException::class)
    fun getColor(name: String): Int {
        e("name = $name")
        return mResources.getColor(
            mResources.getIdentifier(
                appendSuffix(name).also {
                    e("name = $it , $mPluginPackageName")
                },
                DEF_TYPE_COLOR,
                mPluginPackageName
            )
        )
    }

    fun getColorStateList(name: String): ColorStateList? {
        e("name = $name")
        return try {
            mResources.getColorStateList(
                mResources.getIdentifier(
                    appendSuffix(name).also {
                        e("name = $it , $mPluginPackageName")
                    },
                    DEF_TYPE_COLOR,
                    mPluginPackageName
                )
            )
        } catch (e: NotFoundException) {
            e.printStackTrace()
            null
        }
    }

    private fun appendSuffix(name: String): String {
        var name = name
        return if (!TextUtils.isEmpty(mSuffix)) "_" + mSuffix.let { name += it; name } else name
    }

    companion object {
        /**
         * 图片
         */
        private const val DEF_TYPE_DRAWABLE = "drawable"

        /**
         * 颜色
         */
        private const val DEF_TYPE_COLOR = "color"
    }
}